package org.summer.lawyer.controller.wx;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.core.collection.CollUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.summer.common.core.domain.R;
import org.summer.common.mybatis.core.page.PageQuery;
import org.summer.common.mybatis.core.page.TableDataInfo;
import org.summer.lawyer.domain.bo.brokerageuser.BrokerageUserBo;
import org.summer.lawyer.domain.bo.userfollow.UserFollowBo;
import org.summer.lawyer.domain.vo.accountapplication.AccountApplicationCreateVo;
import org.summer.lawyer.domain.vo.brokerageuser.BrokerageUserVo;
import org.summer.lawyer.domain.vo.nimaccount.NimAccountVo;
import org.summer.lawyer.domain.vo.userfollow.UserFollowVo;
import org.summer.lawyer.domain.vo.wxuser.AccountUpdateVo;
import org.summer.lawyer.domain.vo.wxuser.ChatUserVo;
import org.summer.lawyer.domain.vo.wxuser.CustomerServiceVo;
import org.summer.lawyer.domain.vo.wxuser.WxUserVo;
import org.summer.lawyer.service.*;

import java.util.List;

/**
 *
 */
@Tag(name = "微信小程序用户信息接口", description = "微信小程序用户信息接口")
@Slf4j
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/wx/account")
public class WxAccountController {

    private final IWxUserService wxUserService;

    private final IAccountApplicationService accountApplicationService;

    private final IUserFollowService userFollowService;

    private final IBrokerageUserService brokerageUserService;

    private final ILawFirmService lawFirmService;

    private final INimAccountService nimAccountService;

    @Parameters({
            @Parameter(name = "id", description = "用户ID", in = ParameterIn.PATH, required = true)
    })
    @Operation(description = "获取个人信息")
    @GetMapping("/{id}")
    public R<WxUserVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        WxUserVo wxUserVo = wxUserService.selectWxUserById(id);
        if (wxUserVo != null) {
            NimAccountVo nimAccountVo = nimAccountService.selectNimAccountVoByWxUserId(id);
            if (nimAccountVo != null) {
                wxUserVo.setAccid(nimAccountVo.getAccid());
                wxUserVo.setNimToken(nimAccountVo.getToken());
            }
        }
        return R.ok(wxUserVo);
    }

    @Parameters({
            @Parameter(name = "openId", description = "用户openId", required = true)
    })
    @Operation(summary = "查询用户nim账号信息", description = "查询用户nim账号信息, 未找到时，自动创建")
    @SaIgnore
    @GetMapping("/nimAccount/{openId}")
    public R<NimAccountVo> getNimAccount(@NotEmpty(message = "openid不能为空") @PathVariable("openId")String openId){
        return R.ok(nimAccountService.selectNimAccountVoByOpenId(openId));
    }

    @Parameters({
            @Parameter(name = "wxUserId", description = "用户ID", required = true),
            @Parameter(name = "nickname", description = "昵称", required = true)
    })
    @Operation(description = "编辑个人资料")
    @PostMapping("/updateAccount")
    public R<Void> updateAccount(@Validated @RequestBody AccountUpdateVo vo) {
        wxUserService.updateAccount(vo);
        return R.ok();
    }

    @Parameters({
            @Parameter(name = "contactName", description = "姓名", required = true),
            @Parameter(name = "contactPhone", description = "联系方式", required = true)
    })
    @SaIgnore
    @Operation(description = "申请账号")
    @PostMapping("/applyAccount")
    public R<Void> applyAccount(@Validated @RequestBody AccountApplicationCreateVo vo) {
        accountApplicationService.saveAccountApplication(vo);
        return R.ok();
    }

    @Parameters({
            @Parameter(name = "wxUserId", description = "用户ID", in = ParameterIn.PATH, required = true),
    })
    @SaCheckLogin
    @Operation(summary = "用户关注列表", description = "用户关注列表")
    @GetMapping("/listFollow/{wxUserId}")
    public TableDataInfo<UserFollowVo> listFollow(@NotNull(message = "用户ID不能为空") @PathVariable("wxUserId") Long wxUserId) {
        return userFollowService.selectListByWxUserId(wxUserId);
    }

    @Parameters({
            @Parameter(name = "wxUserId", description = "微信用户ID", required = true),
            @Parameter(name = "followeeId", description = "被关注人ID(律所ID)", required = true)
    })
    @Operation(summary = "关注律所和律师", description = "关注律所和律师")
    @SaCheckLogin
    @PostMapping("/follow")
    public R<Void> follow(@Validated @RequestBody UserFollowBo bo) {
        lawFirmService.followLawFirm(bo);
        return R.ok("关注成功");
    }

    @Parameters({
            @Parameter(name = "wxUserId", description = "微信用户ID", required = true),
            @Parameter(name = "followeeId", description = "被关注人ID(律所ID)", required = true)
    })
    @Operation(summary = "取消关注律所和律师", description = "取消关注律所和律师")
    @SaCheckLogin
    @PostMapping("/unfollow")
    public R<Void> unfollow(@Validated @RequestBody UserFollowBo bo) {
        lawFirmService.unfollowLawFirm(bo);
        return R.ok("取消关注成功");
    }


    @SaIgnore
    @Operation(summary = "获取客服电话信息", description = "获取客服电话信息")
    @GetMapping("/getSupport")
    public R<CustomerServiceVo> getSupport() {
        return R.ok(wxUserService.selectCustomerServiceVo());
    }

    @Operation(summary = "获取在线客服IM信息", description = "获取在线客服IM信息")
    @SaIgnore
    @GetMapping("/listChatUser")
    public TableDataInfo<ChatUserVo> listChatUser(){
        List<ChatUserVo> chatUserVos = wxUserService.selectListChatUser();
        if(CollUtil.isNotEmpty(chatUserVos)){
            for (ChatUserVo chatUserVo : chatUserVos) {
                NimAccountVo nimAccountVo = nimAccountService.selectNimAccountVoByUserId(Long.parseLong(chatUserVo.getChatId()));
                if(nimAccountVo != null){
                    chatUserVo.setAccId(nimAccountVo.getAccid());
                    chatUserVo.setNimToken(nimAccountVo.getToken());
                }
            }
        }
        return TableDataInfo.build(chatUserVos);
    }


    @Parameters({
            @Parameter(name = "wxUserId", description = "微信用户ID", in = ParameterIn.PATH, required = true)
    })
    @Operation(summary = "律师端查询我的分销信息", description = "我的分销信息")
    @SaIgnore
    @GetMapping("/brokerageUser/{wxUserId}")
    public R<BrokerageUserVo> getBrokerageUser(@NotNull(message = "用户ID不能为空") @PathVariable("wxUserId") Long wxUserId) {
        return R.ok(brokerageUserService.selectBrokerageUserVoByWxUserId(wxUserId));
    }

    @Parameters({
            @Parameter(name = "bindUserId", description = "上级推广员ID（当前登录用户的用户ID）", required = true),
            @Parameter(name = "pageSize", description = "分页大小", required = true),
            @Parameter(name = "pageNum", description = "当前页数", required = true)
    })
    @Operation(summary = "查询下级分销用户列表", description = "分销下级列表查询")
    @SaCheckLogin
    @GetMapping("/listBrokerageUser")
    public TableDataInfo<BrokerageUserVo> listBrokerageUser(BrokerageUserBo bo, PageQuery pageQuery) {
        if (bo.getBindUserId() == null) {
            bo.setBindUserId(-1L);
        }
        return brokerageUserService.selectPageList(bo, pageQuery);
    }

}
